package com.neusoft.neuiotms.dcam.fixed.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.ContractItemDTO;
import com.neusoft.neuiotms.dcam.fixed.model.AssetType;
import com.neusoft.neuiotms.dcam.fixed.model.Contract;
import com.neusoft.neuiotms.dcam.fixed.model.ContractItem;
import com.neusoft.neuiotms.dcam.fixed.model.Product;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetTypeRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.ContractItemRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.ContractRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.ProductRepository;

import io.micrometer.core.instrument.util.StringUtils;

@Component
public class ContractItemConverter extends AbstractConverter<ContractItem, ContractItemDTO> {

    @Autowired
    private ContractItemRepository contractItemRepository;
    @Autowired
    private AssetTypeRepository assetTypeRepository;
    @Autowired
    private ProductRepository productRepository;
    @Autowired
    private ContractRepository contractRepository;

    @Override
    public ContractItem toModel(final ContractItemDTO dto) {

        ContractItem model;

        if (dto.isNew()) {
            model = new ContractItem();
        } else {
            model = this.contractItemRepository.getOne(dto.getId());
        }

        model.setAmount(dto.getAmount());
        model.setCount(dto.getCount());
        model.setDescription(dto.getDescription());

        // 资产分类
        if (StringUtils.isNotEmpty(dto.getAssetTypeCode())) {
            final AssetType assetType = this.assetTypeRepository.findByCode(dto.getAssetTypeCode());
            model.setAssetType(assetType);
        }

        // 产品
        if (null != dto.getProductId()) {
            final Product product = this.productRepository.getOne(dto.getProductId());
            model.setProduct(product);
        }

        //合同
        if (null != dto.getContractId()) {
            final Contract contract = this.contractRepository.getOne(dto.getContractId());
            model.setContract(contract);
        }

        return model;
    }

    @Override
    public ContractItemDTO toDTO(final ContractItem model, final boolean forListView) {

        final ContractItemDTO dto = new ContractItemDTO();

        dto.setId(model.getId());
        dto.setAmount(model.getAmount());
        dto.setCount(model.getCount());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());
        dto.setDescription(model.getDescription());

        // 产品
        if (model.getProduct() != null) {
            dto.setProductId(model.getProduct().getId());
            dto.setProductName(model.getProduct().getName());
            dto.setStandard(model.getProduct().getSpecification());
        }

        // 合同
        if (model.getContract() != null) {
            dto.setContractId(model.getContract().getId());
            dto.setContractName(model.getContract().getName());
        }

        // 分类
        if (model.getAssetType() != null) {
            dto.setAssetTypeCode(model.getAssetType().getCode());
            dto.setAssetTypeName(model.getAssetType().getName());
        }

        return dto;
    }

}
